﻿$(document).ready(function () {
    page.init();
});

var page = {
    url: "/handler/login.ashx?action=",

    init: function () {
        $("#userName").keyup(function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).css("ime-mode", "disabled");

        $("#btnLogin").click(page.login);
    },

    login: function () {
        var userName = $("#userName").val();
        var password = $("#password").val();

        if ($.trim(userName) == "") {
            alert("请输入帐号");
            $("#userName").focus();
            return;
        }

        if (password == "") {
            alert("请输入密码");
            $("#password").focus();
            return;
        }

        $.post(page.url + "login", { userName: userName, password: password }, function (data) {
            if (data.error) {
                switch (data.error) {
                    case "UserNotFound":
                        alert("帐号不存在，请重新核对后再试");
                        break;
                    case "ForbiddenReferer":
                        alert("账号已被禁止访问，如有疑问请联系管理员");
                        break;
                    case "WrongPassword":
                        alert("密码错误");
                        break;
                }
                return false;
            }
            if (data.success == "NeedUpgradePassword") {
                window.location.href = "/ResetPassword.aspx?first=true&rtn=" + page.rtn;
            }
            else {
                window.location.href = page.rtn;
            }
        }, "json");
    }
};